package com.freightoa.foundation.presentation.composable

import android.content.Context
import android.hardware.input.InputManager
import android.view.inputmethod.InputMethodManager
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.compose.ui.window.DialogProperties
import com.freightoa.base_foundation.R

import com.freightoa.foundation.presentation.theme.Dimen


@Composable
fun DataNotFoundAnim() {
    LabeledAnimation(R.string.data_not_found, R.raw.lottie_error_screen)
}

@Composable
fun UnderConstructionAnim() {
    LabeledAnimation(R.string.under_construction, R.raw.lottie_building_screen)
}

@Composable
fun LoadingDialog(
    isLoading: Boolean,
    animationContent: @Composable () -> Unit = {
        CircularProgressIndicator(
            modifier = Modifier.size(25.dp),
            color = Color.Blue,
            strokeWidth = 4.dp
        )
    }
) {
    if (isLoading) {
        Dialog(
            onDismissRequest = { /* 禁止点击外部关闭 */ },
            properties = DialogProperties(
                dismissOnBackPress = false,    // 禁止返回键关闭
                dismissOnClickOutside = false, // 禁止点击外部关闭
                usePlatformDefaultWidth = false
            )
        ) {
            Box(
                modifier = Modifier
                    .fillMaxSize()
                    .background(Color.Transparent) // 修改背景透明度为 0.2
                    .pointerInput(Unit) { detectTapGestures {} },
                contentAlignment = Alignment.Center // 让内容居中显示
            ) {
                Box(
                    modifier = Modifier
                        .wrapContentSize()
                        .background(Color.Transparent)
                )
                {
                    animationContent()
                }
            }
        }
    }
}


@Composable
fun ErrorDialog(
    modifier: Modifier = Modifier,
    showError: Boolean,
    onDismiss: () -> Unit,
    title: String="",
    message: String,
    confirmButtonText: String = "confirm",
) {
    if (showError) {
        Dialog(
            onDismissRequest = { /* 禁用外部点击关闭 */ },
            properties = DialogProperties(
                dismissOnBackPress = false,
                dismissOnClickOutside = false
            )
        ) {
            Surface(
                modifier = modifier
                    .width(280.dp)
                    .clip(MaterialTheme.shapes.medium),
                color = MaterialTheme.colorScheme.surface
            ) {
                Column(
                    modifier = Modifier.padding(15.dp),
                    horizontalAlignment = Alignment.CenterHorizontally
                ) {
//                    // 错误图标
//                    Icon(
//                        imageVector = Icons.Default.Error,
//                        contentDescription = null,
//                        tint = MaterialTheme.colorScheme.error,
//                        modifier = Modifier.size(48.dp)
//                    )

                   // Spacer(modifier = Modifier.height(16.dp))

                    // 标题
//                    Text(
//                        text = title,
//                        style = MaterialTheme.typography.titleMedium,
//                        color = MaterialTheme.colorScheme.onSurface
//                    )

                    Spacer(modifier = Modifier.height(8.dp))

                    // 错误信息
                    Text(
                        text = message,
                        style = MaterialTheme.typography.bodyMedium,
                        color = MaterialTheme.colorScheme.onSurfaceVariant,
                        textAlign = TextAlign.Center
                    )

                    Spacer(modifier = Modifier.height(24.dp))

                    // 确认按钮
                    Button(
                        onClick = onDismiss,
                        modifier = Modifier.fillMaxWidth(),
                        shape = RoundedCornerShape(8.dp),
                        colors = ButtonDefaults.buttonColors(
                            containerColor = MaterialTheme.colorScheme.primary
                        )
                    ) {
                        Text(text = confirmButtonText)
                    }
                }
            }
        }
    }
}

// 使用示例
@Composable
fun DemoErrorDialog() {
    var showError by remember { mutableStateOf(false) }

    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Button(onClick = { showError = true }) {
            Text("触发错误")
        }

        ErrorDialog(
            showError = showError,
            onDismiss = { showError = false },
            title = "操作失败",
            message = "网络连接异常，请检查网络设置后重试"
        )
    }
}

@Composable
fun ProgressIndicatorNow(isLoading: Boolean) {
    if (isLoading) {
        val context = LocalContext.current
        hideKeyboards(context)
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier
                .fillMaxSize()
                .pointerInput(Unit) {} // 忽略触摸事件
                .pointerInput(Unit) { detectTapGestures {} }
                .clickable(enabled = false) {},
        ) {
            CircularProgressIndicator(
                modifier = Modifier
                    .align(Alignment.Center)
                    .size(Dimen.spaceL),
            )
        }
    }

}

@Composable
fun ProgressIndicator() {
    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier.fillMaxSize(),
    ) {
        CircularProgressIndicator(
            modifier = Modifier
                .align(Alignment.Center)
                .size(Dimen.spaceL),
        )
    }
}
